<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="title">
          <span
            @click="
              ;((page_data.logType = 1), (page_data.current = 1)), get_list()
            "
            :class="page_data.logType == 1 ? 'select' : ''"
            >登录日志</span
          >
          <span
            @click="
              ;((page_data.logType = 2), (page_data.current = 1)), get_list()
            "
            :class="page_data.logType == 2 ? 'select' : ''"
            >操作日志</span
          >
        </div>
        <div class="flex">
          <!-- <el-button type="primary" @click="show_box = true">新增</el-button> -->

          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        highlight-current-row
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column type="expand">
          <template #default="props">
            <div>
              <div>
                <span class="dian"></span>请求参数：{{ props.row.args }}
              </div>
              <div class="flex">
                <span
                  class="dian"
                  :style="{
                    backgroundColor:
                      props.row.responseContent?.code == 200
                        ? '#34CE80'
                        : '#EF5656',
                  }"
                ></span>
                <div class="flex">
                  返回参数：{ code:{{ props.row.responseContent?.code }}, data:
                  <div class="response_data">
                    <el-tooltip
                      placement="bottom-start"
                      :content="props.row.responseContent?.data"
                    >
                      {{ props.row.responseContent?.data }}
                    </el-tooltip>
                  </div>
                  , message:{{ props.row.responseContent?.message }}}
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="日志内容" v-if="header_list[0].showTable">
          <template #default="scope: any">
            <div class="text_over">
              <el-tooltip :content="scope.row.content" placement="bottom">
                {{ scope.row.content }}
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          v-if="header_list[1].showTable"
          label="操作人ID"
          prop="operatorId"
          width="180px"
        ></el-table-column> -->
        <el-table-column
          label="操作人名称/账号"
          prop="operator"
          v-if="header_list[1].showTable"
        >
          <template #default="scope: any">
            <div v-if="scope.row.operator != ''">
              {{ scope.row.operator }}
            </div>
            <div v-else></div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作时间"
          prop="time"
          width="170px"
          v-if="header_list[2].showTable"
        >
          <template #default="scope: any">{{
            formatDate(parseInt(scope.row.operatingTime), 'yyyy-MM-dd hh:mm:ss')
          }}</template>
        </el-table-column>
        <el-table-column
          label="IP"
          prop="ip"
          v-if="header_list[3].showTable"
        ></el-table-column>
        <el-table-column
          label="IP地址"
          prop="ipAddress"
          v-if="header_list[4].showTable"
        ></el-table-column>
        <el-table-column
          v-if="header_list[5].showTable"
          label="耗时(毫秒)"
          width="100px"
          prop="timeConsuming"
        ></el-table-column>
        <el-table-column
          label="操作类型"
          v-if="page_data.logType == 2 && header_list[6].showTable"
          width="80px"
        >
          <template #default="scope: any">
            <div v-for="item in operateType_dict_list" :key="item.id">
              <span v-if="item.dictCode == scope.row.operatingType">{{
                item.dictValue
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="设备类型"
          width="80px"
          v-if="page_data.logType == 1 && header_list[6].showTable"
        >
          <template #default="scope: any">
            <div v-for="item in deviceType_dict_list" :key="item.id">
              <span v-if="scope.row.deviceType == item.dictCode">{{
                item.dictValue
              }}</span>
            </div>
          </template></el-table-column
        >
        <el-table-column
          label="登录方式"
          v-if="page_data.logType == 1 && header_list[7].showTable"
          width="80px"
        >
          <template #default="scope: any">
            <div v-for="item in loginType_dict_list" :key="item.id">
              <span v-if="scope.row.loginMethod == item.dictCode">{{
                item.dictValue
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="登录企业"
          v-if="page_data.logType == 2 && header_list[7].showTable"
        >
          <template #default="scope: any">
            <div v-if="scope.row.enterpriseName != ''">
              {{ scope.row.enterpriseName }}
            </div>
            <div v-else>--</div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="
          (e) => {
            page_data.pageSize = e
            get_list()
          }
        "
        @current-change="
          (e) => {
            page_data.current = e
            get_list()
          }
        "
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { dictData, formatDate } from '@/utils/utils' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { useRoute } from 'vue-router'
const route = useRoute()
// 该页面权限组
const list = ref()
const total = ref()
const loading = ref(false)
const height_size = ref(1)
const header_list = ref([
  {
    label: '日志内容',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  //   {
  //     label: '操作人ID',
  //     value: '',
  //     showTable: true,
  //     type: 'input',
  //     isShow: true,
  //     options: [],
  //   },
  {
    label: '操作人名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '操作时间',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
  {
    label: 'IP',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: 'IP地址',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '耗时(毫秒)',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: computed(() => {
      return page_data.value.logType == 1 ? '设备类型' : '操作类型'
    }),
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: computed(() => {
      return page_data.value.logType == 1
        ? deviceType_dict_list.value
        : operateType_dict_list.value
    }),
  },
  {
    label: computed(() => {
      return page_data.value.logType == 1 ? '登录方式' : '登录企业'
    }),
    value: '',
    showTable: true,
    type: computed(() => {
      return page_data.value.logType == 1 ? 'select' : 'input'
    }),
    isShow: false,
    options: computed(() => {
      return page_data.value.logType == 1 ? loginType_dict_list.value : ''
    }),
  },
])
const page_data = ref({
  platformType: 1,
  logType: 1,
  current: 1,
  pageSize: 10,
  content: '',
  deviceType: '', // 设备类型
  ip: '',
  ipAddress: '',
  loginMethod: '',
  operatingTime: '',
  operator: '',
  operatorId: '',
  timeConsuming: '',
  operatingType: '', // 操作类型
  enterpriseName: '',
})

const operateType_dict_list = ref([])
const loginType_dict_list = ref([])
const deviceType_dict_list = ref([])
onMounted(() => {
  // 根据 路由 区分
  if (route.name == 'sysNote') {
    // 平台
    page_data.value.platformType = 1
  } else if (route.name == 'enterpriseNote') {
    // 企业
    page_data.value.platformType = 2
  }
  max_height.value = window.innerHeight - 320 // 不带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  get_list()
  dictData('1547113490500554754').then((res) => {
    operateType_dict_list.value = res as any
    operateType_dict_list.value.forEach((item) => {
      item.label = item.dictValue
      item.value = item.dictCode
    })
  }) // 获取字典值 操作类型
  dictData('1547113126539825153').then((res) => {
    loginType_dict_list.value = res as any
    loginType_dict_list.value.forEach((item) => {
      item.label = item.dictValue
      item.value = item.dictCode
    })
  }) // 获取字典值 登录方式
  dictData('1547112907832037378').then((res) => {
    deviceType_dict_list.value = res as any
    deviceType_dict_list.value.forEach((item) => {
      item.label = item.dictValue
      item.value = item.dictCode
    })
  }) // 获取字典值 设备类型
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const max_height = ref()
const get_list = () => {
  loading.value = true
  axios
    .request({
      url:
        page_data.value.logType == 1
          ? serviceApi.log_list
          : serviceApi.operate_list,
      method: 'post',
      data: page_data.value,
    })
    .then((res) => {
      console.log(res)
      let data_list = res.data.content
      if (data_list.length) {
        data_list.forEach((i) => {
          i.responseContent =
            i.responseContent == '' ? '' : JSON.parse(i.responseContent)
        })
      }
      console.log(data_list)
      list.value = data_list
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      loading.value = false
      ElMessage.error(err)
    })
}
const search_key = () => {
  page_data.value = {
    platformType: page_data.value.platformType,
    logType: page_data.value.logType,
    current: 1,
    pageSize: 10,
    content: header_list.value[0].value,
    deviceType: page_data.value.logType == 1 ? header_list.value[7].value : '', // 设备类型
    ip: header_list.value[4].value,
    ipAddress: header_list.value[5].value,
    loginMethod: page_data.value.logType == 2 ? '' : header_list.value[8].value,
    operatingTime: header_list.value[3].value,
    operator: header_list.value[2].value,
    operatorId: header_list.value[1].value,
    timeConsuming: header_list.value[6].value,
    operatingType:
      page_data.value.logType == 2 ? header_list.value[7].value : '', // 操作类型
    enterpriseName:
      page_data.value.logType == 2 ? header_list.value[8].value : '',
  }
  get_list()
}
</script>

<style lang="scss" scoped>
.response_data {
  max-width: 500px !important;
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
:deep(.el-table__expanded-cell) {
  background-color: #f0f2f5;
  padding-left: 100px;
  div {
    padding: 5px 0;
  }
  .dian {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 5px;
    &:first-child {
      background-color: #3580eb;
    }
  }
}
.title {
  font-weight: bold;
  span {
    cursor: pointer;
    padding: 10px;
  }
  .select {
    color: #3580eb;
  }
}
</style>
